#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll n;
ll x[500001], y[500001];
unordered_map<ll, int>mp;
ll ds(ll x, ll y) {
	return x * 500001 + y;
}
queue<ll>p;
ll vis[4001][4001];
void solve0() {
	sort(x + 1, x + 1 + n);
	sort(y + 1, y + 1 + n);
	ll ans = 0, sum = 0;
	for (int i = 1; i <= n; i++) {
		ans += (x[i] + y[i]) * (i - 1) - sum;
		sum += x[i] + y[i];
	}
	cout << ans;
}
ll res = 0;
bool lg[500001];
ll ddd[500001][2];
vector<ll>son[500001];
void dfs(ll d) {
	lg[d] = 1;
	for (int j = -1; j <= 1; j++)
		for (int k = -1; k <= 1; k++) {
			if ((j == 0 || k == 0) && (j != 0 || k != 0)) {
				ll e = mp[ds(x[d] + j, y[d] + k)];
				if (e != 0 && lg[e] == 0) {
					dfs(e);
					son[d].push_back(e);
				}
			}
		}
	for (unsigned int j = 0; j < son[d].size(); j++)
		ddd[d][0] += ddd[son[d][j]][0], ddd[d][1] += ddd[son[d][j]][0] + ddd[son[d][j]][1];
	ll ans = 0;
	for (unsigned int j = 0; j < son[d].size(); j++)
		ans = ans + (ddd[son[d][j]][1] + ddd[son[d][j]][0]) * (ddd[d][0] - ddd[son[d][j]][0]);
	res += ans;
	for (unsigned int j = 0; j < son[d].size(); j++)
		res += ddd[son[d][j]][1] + ddd[son[d][j]][0];
	ddd[d][0]++;
}
void solve1() {
	dfs(1);
//	cout<<91;
	cout << res;
}
int main() {
	freopen("game.in", "r", stdin);
	freopen("game.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> x[i] >> y[i];
		mp[ds(x[i], y[i])] = i;
	}
	if (n <= 4000) {
		ll ans = 0;
		for (int i = 1; i <= n; i++) {
			p.push(i);
			while (p.size()) {
				ll d = p.front();
				p.pop();
				for (int j = -1; j <= 1; j++)
					for (int k = -1; k <= 1; k++) {
						if ((j == 0 || k == 0) && (j != 0 || k != 0)) {
							ll e = mp[ds(x[d] + j, y[d] + k)];
							if (e != 0 && vis[i][e] == 0 && i != e) {
								p.push(e);
								vis[i][e] = vis[i][d] + 1;
							}
						}
					}
			}
			for (int j = i; j <= n; j++)
				ans += vis[i][j];
		}
		cout << ans;
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		if (mp[ds(x[i], y[i])] && mp[ds(x[i] + 1, y[i])] && mp[ds(x[i], y[i] + 1)] && mp[ds(x[i] + 1, y[i] + 1)]) {
			solve0();
			return 0;
		}
	}
	solve1();
	return 0;
}
